package com.pacyki.database;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DbAdapter {
	
	//private final Context mCtx;
	private static final String TAG = "DbAdapter";
	private DatabaseHelper mDbHelper;
//	private SQLiteDatabase mDb;
//	
//	Database creation sql statement
//	
	private static final String DATABASE_CREATE = 
		"CREATE TABLE BgCategories (bgcid INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL)"
	  + "CREATE TABLE BgImages (bgiid INTEGER PRIMARY KEY AUTOINCREMENT, bgcid INTEGER NOT NULL, imageName TEXT NOT NULL, resName TEXT NOT NULL)"
	  + "CREATE TABLE TextCategories (tcid INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL)"
	  + "CREATE TABLE Texts (tid INTEGER PRIMARY KEY AUTOINCREMENT, tcid INTEGER NOT NULL, text TEXT NOT NULL)"
	  + "CREATE TABLE Templates (tlid INTEGER PRIMARY KEY AUTOINCREMENT, biid INTEGER NOT NULL, tid INTEGER NOT NULL, textAlign TEXT NOT NULL, byteArrayPath TEXT NOT NULL, recipients TEXT NOT NULL)"
	  + "INSERT INTO BgCategories (name) values (\"birthdays\")"
	  + "INSERT INTO BgCategories (name) values (\"weddings\")"
	  + "INSERT INTO BgCategories (name) values (\"march 8th\")"
	  + "INSERT INTO BgImages (bgcid, imageName, resName) values (1, \"red\", \"red.png\")"
	  + "INSERT INTO BgImages (bgcid, imageName, resName) values (1, \"green\", \"hearts_green.png\")"
	  + "INSERT INTO BgImages (bgcid, imageName, resName) values (2, \"blue\", \"blue.png\")"
	  + "INSERT INTO BgImages (bgcid, imageName, resName) values (2, \"vlad\", \"rabbit.png\")"
	  + "INSERT INTO BgImages (bgcid, imageName, resName) values (3, \"man\", \"8march.png\")"
	  + ";";
	private static final String DATABASE_NAME = "Cards";
	private static final String DATABASE_BG_CAT_TABLE = "BgCategories";
	private static final String DATABASE_BG_IMAGES_TABLE = "BgImages";
	private static final String DATABASE_TEXT_CATEGORIES_TABLE = "TextCategories";
	private static final String DATABASE_TEXTS_TABLE = "Texts";
	private static final String DATABASE_TEMPLATES_TABLE = "Templates";
	private static final int DATABASE_VERSION = 2;
	
	private static class DatabaseHelper extends SQLiteOpenHelper {
		
		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(DATABASE_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS " + DATABASE_BG_CAT_TABLE + ", " + DATABASE_BG_IMAGES_TABLE + ", " + DATABASE_TEXT_CATEGORIES_TABLE + ", " + DATABASE_TEXTS_TABLE + ", " + DATABASE_TEMPLATES_TABLE + ";");
			onCreate(db);
		}
	}
	
	public DbAdapter(Context ctx) {
		//this.mCtx = ctx;
		mDbHelper = new DatabaseHelper(ctx);
	}
	
	private SQLiteDatabase openForWrite() throws SQLException {
		this.close();
		SQLiteDatabase mDb = mDbHelper.getWritableDatabase();
		return mDb;
	}
	
	private SQLiteDatabase openForRead() throws SQLException {
		this.close();
		SQLiteDatabase mDb = mDbHelper.getReadableDatabase();
		return mDb;
	}
	
	public Cursor getBGCategories() {
		SQLiteDatabase mDb = this.openForRead();
		Cursor cursor = mDb.query(DATABASE_BG_CAT_TABLE, null, null, null, null, null, null);
		mDb.close();
		this.close();
		return cursor;
	}
	
	public Cursor getBGCategory(int id) {
		SQLiteDatabase mDb = this.openForRead();
		Cursor cursor = mDb.query(DATABASE_BG_CAT_TABLE, null, "bgcid=?", new String[] { Integer.toString(id) }, null, null, null);
		mDb.close();
		this.close();
		return cursor;
	}
	
	private void close() {
		mDbHelper.close();
	}
	
}
